import textHtml from 'ngtemplate!html!../../../tpls/validation/text.html';
import phoneHtml from 'ngtemplate!html!../../../tpls/validation/phone.html';
import clubsDialogHtml from 'ngtemplate!html!../../../tpls/company/clubs.dialog.tpl.html';

class CompanyCreateController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, CompanyService, fileReader, iRunner){

        var vm = this;

    	vm.$rootScope     = $rootScope;
        vm.$scope         = $scope;
    	vm.$cookieStore   = $cookieStore;
    	vm.$timeout 	  = $timeout;
    	vm.$state   	  = $state;
    	vm.ngDialog 	  = ngDialog;
    	vm.dialogService  = dialogService;
        vm.CompanyService = CompanyService;
    	vm.fileReader     = fileReader;
    	vm.iRunner 	      = iRunner;

    	vm.textHtml  = textHtml;
    	vm.phoneHtml = phoneHtml;

        // 超级管理员
        vm.is_admin = $rootScope.is_admin;
                
        // 机构类型
        vm.company_types = iRunner.raceScaleConfig;

        // 表单数据初始化
        vm.company = {
            company_name: '',
            company_city: '',
            company_num: '',
            radio_num: '',
            company_scale: '',
            radio_scale: '',
            company_type: '',
            company_phone: '',
            company_email: ''
        }


        // 获取是否有跑团已经创建
        vm.CompanyService.getClubs(vm.$cookieStore.get('access_token'))
        .then( (data) => {
            console.log(data.data.length);
            // 调用接口成功的场合 并且 已经又自己所属的跑团的场合
            if (data.code == 0 && data.data.length != 0) {
                // 打开模态框
                $rootScope.clubs = data.data;
                vm.ngDialog.open({
                    template: clubsDialogHtml,
                    className: 'ngdialog-theme-default ngdialog-alert',
                    scope: $rootScope
                });

            }

            console.log(data)
        }, (data) => {
            console.log('error')
        });

        // 一键创建跑团
        $rootScope.createFromClub = function(id) {
            // 没有选择跑团的场合
            if (!id) {
                vm.dialogService.error("请选择跑团");
            } else {
                // 一键创建公司
                var params = {
                    club_id: id
                }
                vm.CompanyService.postCompanyCreate(params, vm.$cookieStore.get('access_token'))
                .then( (data) => {
                    // 判断用户是否创建成功
                    data.code==0&&vm.dialogService.success('创建成功', 'dashboard.companylist');
                    // 调用失败, 提示用户
                    (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);
                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);
                    console.log(data)
                }, (data) => {
                    console.log('error')
                });
            }
        }

        $rootScope.close = function() {
            vm.ngDialog.close();
        }


        // 监听模型变化, 显示每年赛事场戏
        $scope.$watch(angular.bind(vm, () => vm.company.radio_num ), (newValue, oldValue) => {
            if (newValue) {
                if (newValue==0) {
                    vm.company.company_num = iRunner.raceFrequencyConfig.number[0];
                }
                if (newValue==1) {
                    vm.company.company_num = iRunner.raceFrequencyConfig.number[1];
                }
                if (newValue==2) {
                    vm.company.company_num = iRunner.raceFrequencyConfig.number[2];
                }
                console.log(newValue)
            }
        });

        // 监听模型变化, 显示主要赛事规模
        $scope.$watch(angular.bind(vm, () => vm.company.radio_scale ), (newValue, oldValue) => {
            if (newValue) {
                if (newValue==0) {
                    vm.company.company_scale = iRunner.raceFrequencyConfig.scale[0];
                }
                if (newValue==1) {
                    vm.company.company_scale = iRunner.raceFrequencyConfig.scale[1];
                }
                if (newValue==2) {
                    vm.company.company_scale = iRunner.raceFrequencyConfig.scale[2];
                }
                if (newValue==3) {
                    vm.company.company_scale = iRunner.raceFrequencyConfig.scale[3];
                }
                console.log(newValue)
            }
        });

    }

    // 表单提交
    submitForm(isValid) {
        var vm = this;
        // 验证失败, 提示用户
        if (!isValid) {

            if (!vm.company.company_name) {
                $.blink(angular.element(document.getElementsByName('companyName')), 5, 100)
            } else if (!vm.company.company_type) {
                $.blink(angular.element(document.getElementsByName('companyType')), 5, 100)
            } else if (!vm.company.company_phone) {
                $.blink(angular.element(document.getElementsByName('companyPhone')), 5, 100)
            }

        }

        // 验证通过
        if (isValid) {

            // 拼接POST数据格式
            var params = {
                company_name: vm.company.company_name,
                type: vm.company.company_type,
                logo: vm.myFile,
                city: vm.company.company_city,
                user_mail: vm.company.company_email,
                user_cell: vm.company.company_phone,
                race_scale: vm.company.company_scale,
                race_frequency: vm.company.company_num
            }

            console.log(params)

            // check机构名称相同
            vm.CompanyService.getCompanyExists(vm.$cookieStore.get('access_token'), vm.company.company_name)
            .then( (data) => {

                // 调用接口正确的场合
                if (data.code == 0) {
                    // 没有重复的机构名称的场合
                    if (data.data != true) {
                        // 创建公司
                        vm.CompanyService.postCompanyCreate(params, vm.$cookieStore.get('access_token'))
                        .then( (data) => {

                            // 判断用户是否创建成功
                            data.code==0&&vm.dialogService.success('创建成功', 'dashboard.companylist');

                            // 调用失败, 提示用户
                            (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                            // 登录信息已过期, 请重新登录
                            data.code==-1001&&vm.dialogService.error(data.message, true);

                            console.log(data)
                        }, (data) => {
                            console.log('error')
                        });
                    } else {
                        // 有重复机构抿成的场合
                        vm.dialogService.error("机构名称已存在，请重新输入机构名称。");
                    }

                } 

                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);

                console.log(data)
            }, (data) => {
                console.log('error')
            });

        }

        return false;
    }

    // 获取上传图片的地址url，用以实现预览图片
    getFile() {
        var vm = this;
        vm.fileReader.readAsDataUrl(vm.$scope.file, vm.$scope)
        .then( (result) => {
            console.log(vm.$scope.file)

            //判断附件是否为图片
            if(!/image\/\w+/.test(vm.$scope.file.type)){
                vm.dialogService.error('图片类型必须是.gif,jpeg,jpg,png中的一种哦');
                return false;
            } else {
                vm.imageSrc = result;
            }
            
        }, (data) => {
            console.log('error')
        });
    }
}

CompanyCreateController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'CompanyService', 'fileReader', 'iRunner']; 

export default CompanyCreateController;